package com.sen.pro.switches.admin.common;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sen.common.struts.BaseAction;
import com.sen.common.util.JsonBinder;
import com.sen.common.util.web.Struts2Utils;

/**
 * 供 KindEditor在线html编辑器调用
 * 
 * @author tian qingsen
 */
@Scope("prototype")
@Controller("kindEditorAction")
@Namespace("/ke")
public class KindEditorAction extends BaseAction {

	private static final long serialVersionUID = -583704065735235979L;
	private Logger LOG = Logger.getLogger(KindEditorAction.class);

	private File imgFile;

	private String contentType;

	private String fileName;

	private final static Long FAILED = 1L;

	private final static Long SUCCESS = 0L;

	private final static long MAXSIZE = 5 * 1024 * 1024;

	private final static String fileTypes = "^(jpg|png|jpeg)$";

	private final static String filetypeMsg = "jpg,png,jpeg";

	/**
	 * KindEditor上传使用
	 * @return
	 */
	@Action(value = "upload")
	public String upload() {
		getMessage(SUCCESS, "提示信息");
		return NONE;
	}

	/**
	 * 输出消息
	 * @param error
	 * @param message
	 */
	private void getMessage(Long error, String message) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("error", error);
		if (SUCCESS.equals(error)) {
			map.put("url", message);
		} else {
			map.put("message", message);
		}
		Struts2Utils.renderHtml(JsonBinder.buildNormalBinder().toJson(map));
	}

	public File getImgFile() {
		return imgFile;
	}

	public void setImgFile(File imgFile) {
		this.imgFile = imgFile;
	}

	public String getContentType() {
		return contentType;
	}

	public void setImgFileContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getFileName() {
		return fileName;
	}

	public void setImgFileFileName(String fileName) {
		this.fileName = fileName;
	}
}
